
import http from './http.js';
import WXBizDataCrypt from "./WXBizDataCrypt.js"
function getUserInfo(succ) {
	let profile = uni.getStorageSync("profile")
	console.log('profile empty')
	console.log(profile)
	if (profile == void 0 || profile.length == 0) {
		// uni.showLoading({
		// 	title:"正在登录"
		// })
		//获取用户信息
		//getUserProfile 仅微信支持
		if (uni.getUserProfile) {
			uni.getUserProfile({
				desc:"注册",
				success(res) {
					console.log(res)
					uni.hideLoading()
					uni.setStorageSync("profile",res.userInfo)
					succ(res.userInfo)
				},
				fail(err) {
					uni.hideLoading()
					console.log(err)
				}
			})
		}else{
			uni.getUserInfo({
				success(res) {
					console.log(res)
					uni.hideLoading()
					uni.setStorageSync("profile",res.userInfo)
					succ(res.userInfo)
				},
				fail(err) {
					uni.hideLoading()
					console.log(err)
					
				}
			})
		}
		

	}else{
		console.log('profile existed')
		succ(profile)
	}
}

function getLoginCode(succ) {
	uni.login({
		success(res) {
			succ(res.code)
		}
	})
}

function isLogin() {
	var token = uni.getStorageSync("token")
	if (token == void 0 || token.length == 0) {return false}
	return true
}

function isUserInfo() {
	let profile = uni.getStorageSync("profile")
	if (profile == void 0 || profile.length == 0) {
		return false
	}
	return true
}

function isBindPhone() {
	let phone = uni.getStorageSync("phone")
	if (phone == void 0 || phone.length == 0) {
		return false
	}
	return true
}

function loginIfNeed(succ){
	wx.getStorageSync()
	var token = uni.getStorageSync("token")
	var isLogin = true
	if (token == void 0 || token.length == 0) {isLogin=false}
	if (isLogin) {
		succ()
	}else{
		uni.login({
			// provider:"weixin",
			success(res) {
				console.log(res)
				http.post("/weixin/code", {"code":res.code}, (r)=>{
					console.log("auth Token:"+r.userinfo.Token)
					uni.setStorageSync("token", r.userinfo.Token)
					uni.setStorageSync("uid", r.userinfo.Id)
					succ(r)
				}, "加载中")
			}
		})
	}
}

function decryptData(encryptedData, iv){
	var sessionKey = uni.getStorageSync("skey")
	console.log(sessionKey)
	var appId = "wx09cd63acf1709730"
	var pc = new WXBizDataCrypt(appId, sessionKey)
	var data = pc.decryptData(encryptedData , iv)
	console.log('解密后 data: ', data)
	return data
}

function updateUserInfo() {

}

function formatTime(timestamp) {
	
	var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
	var Y = date.getFullYear() + '-';
	var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1):date.getMonth()+1) + '-';
	var D = (date.getDate()< 10 ? '0'+date.getDate():date.getDate())+ ' ';
	var h = (date.getHours() < 10 ? '0'+date.getHours():date.getHours())+ ':';
	var m = (date.getMinutes() < 10 ? '0'+date.getMinutes():date.getMinutes()) + ':';
	var s = date.getSeconds() < 10 ? '0'+date.getSeconds():date.getSeconds();
	return Y+M+D+h+m+s;
}

function getUrlParams(url) {
	console.log(url)
	let querys = url.split("?");
	if (querys.length < 2) {
		return []
	}
	let query = url.split("?")[1];
	let zparams = query.split("&");
	var params = {}
	for (var i=0;i<zparams.length;i++) {
		var sp = zparams[i].split("=")
		params[sp[0]] = sp[1]
	}
	
	return params
}

function getPlatform(){
	let platForm = void 0
	// #ifdef MP-WEIXIN
	platForm = "mp-weixin"
	// #endif
	
	// #ifdef MP-ALIPAY
	platForm = "mp-alipay"
	// #endif
	
	return platForm
}

function getSystemInfo() {
	var systemInfo = uni.getSystemInfoSync()
	// #ifdef MP-WEIXIN
	let menuButtonObject = uni.getMenuButtonBoundingClientRect();
	systemInfo['titleBarHeight'] = menuButtonObject.height
	// #endif
	
	return systemInfo
}

function getKefuPhone(succ) {
	http.get("/customerphone", {}, (r)=>{
		console.log(r)
		succ(r)
	}, "正在获取")
}

function showKefu(succ) {
	getKefuPhone((r)=>{
		var ph = r
		uni.showActionSheet({
			title:ph,
			itemList:[
				"呼叫"
			],
			complete(res) {
				if (res.tapIndex == 0) {
					uni.makePhoneCall({
						phoneNumber:ph
					})
				}
			}
		})
	})
}

export default {
	getUserInfo,
	getLoginCode,
	isLogin,
	loginIfNeed,
	isUserInfo,
	isBindPhone,
	decryptData,
	formatTime,
	getUrlParams,
	getPlatform,
	getSystemInfo,
	getKefuPhone,
	showKefu
}